package com.myapplication.http;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.myapplication.bean.Banner;
import com.myapplication.bean.DeatilBanner1;
import com.myapplication.bean.ListBean;
import com.myapplication.bean.ShaiwuBean;
import com.myapplication.bean.Shop;
import com.myapplication.bean.ZiXunBean;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by zitaojiang on 2016/10/8.
 */
public class HttpUtils {
   //创建HttpUtils对象
   private static volatile HttpUtils singletion;
   private Retrofit mRetrofit;
   private RetrofitService mRetrofitService;
   private HttpUtils(){

   }
   public static HttpUtils getSingletion(){
      if(singletion==null){
         synchronized (HttpUtils.class){
            if(singletion==null){
               singletion=new HttpUtils();
            }
         }
      }
      return singletion;
   }
   //自定义retrofit实例
   private Retrofit createRetrofit(String baseUrl){
      Log.i("TAG","baseuri------->"+baseUrl);
      return new Retrofit.Builder()
              .baseUrl(baseUrl)
              .addConverterFactory(GsonConverterFactory.create())
              .build();
   }
   /**
    *创建gson实例，用于retrofit解析返回的结果
    */
   private Gson createGson(){
      Gson gson=new GsonBuilder()
              .serializeNulls()
              .create();
      return gson;
   }

   /**
    * 创建网络接口的实例
    *
    * @return
    */
   private RetrofitService createPresent(){
      RetrofitService mRetrofitService=mRetrofit.create(RetrofitService.class);
      return mRetrofitService;
   }

   public  void  getBanner1(String baseUrl, Callback<DeatilBanner1> callback){
      mRetrofit=createRetrofit(baseUrl);
      mRetrofitService=createPresent();
      Call<DeatilBanner1> call=mRetrofitService.getData_Banner1();
      call.enqueue(callback);
   }

   public  void  getBannerData(String baseUrl, Callback<Banner> callback){
      mRetrofit =createRetrofit(baseUrl);
      mRetrofitService =createPresent();
      Call<Banner> call=mRetrofitService.getBannerData();
      call.enqueue(callback);
   }

   public void getShopData(String baseUrl, Map<String,String> parmas, Callback<Shop> callback){
      mRetrofit=createRetrofit(baseUrl);
      mRetrofitService =createPresent();
      //利用mRetrofitService里面的方法获得retrofit的回调
      Call<Shop> call=mRetrofitService.getShopData(parmas);
      //通过异步在请求的队列enqueue中的到接口回调的结果
      // call.execute()方法是同步
      call.enqueue(callback);

   }
   //网络访问获取晒物数据的方法
   public void getShaiWuData(String baseUrl, Map<String,String> parmas, Callback<ShaiwuBean> callback){
      mRetrofit=createRetrofit(baseUrl);
      mRetrofitService =createPresent();
      Call<ShaiwuBean> call=mRetrofitService.getShaiWuData(parmas);
      call.enqueue(callback);
   }
   //网络访问获取资讯数据的方法
   public void getZiXunData(String baseUrl,Map<String,String> params,Callback<ZiXunBean> callback){
      mRetrofit=createRetrofit(baseUrl);
      mRetrofitService =createPresent();
      Call<ZiXunBean> call=mRetrofitService.getZiXunData(params);
      call.enqueue(callback);
   }
   //网络获取清单数据的方法
   public void getListData(String baseUrl, Map<String,String> params, Callback<ListBean> callback){
      mRetrofit=createRetrofit(baseUrl);
      mRetrofitService=createPresent();
      Call<ListBean> call=mRetrofitService.getListData(params);
      call.enqueue(callback);
   }
   //网络获取购物清单数据的方法
//   public void getShopListData(String baseUrl,String type, Map<String,String> params, Callback<ShopListBean> callback){
//      mRetrofit=createRetrofit(baseUrl);
//      mRetrofitService=createPresent();
//      Call<ShopListBean> call=mRetrofitService.getShopListData(type,params);
//      call.enqueue(callback);
//   }
}
